package com.daxia.bumall.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Repository;

import com.daxia.bumall.common.HardcoverStyleType;
import com.daxia.bumall.daobase.GenericDAOHibernate;
import com.daxia.bumall.dto.BrandStyleDTO;
import com.daxia.bumall.model.BrandStyle;
import com.daxia.bumall.support.Page;

@Repository
public class BrandStyleDAO extends GenericDAOHibernate<BrandStyle> {

    public List<BrandStyle> find(BrandStyleDTO dto, Page page) {
        StringBuilder hql = new StringBuilder();
        List<Object> paras = new ArrayList<Object>();
        hql.append("From BrandStyle n where 1 = 1 ");
        if (dto.getId() != null) {
            hql.append(" and n.id = ? ");
            paras.add(dto.getId());
        }
        if (dto.getBrand() != null) {
        	if (dto.getBrand().getId() != null) {
	            hql.append(" and n.brand.id = ? ");
	            paras.add(dto.getBrand().getId());
        	}
        }			
        if (dto.getHouseStyle() != null) {
        	if (dto.getHouseStyle().getId() != null && dto.getHouseStyle().getId().longValue() > 0) {
	            hql.append(" and n.houseStyle.id = ? ");
	            paras.add(dto.getHouseStyle().getId());
        	}
        }	
        if (dto.getStyleID() != null && dto.getStyleID().longValue() > 0) {
        	if (dto.getStyleID().longValue() == HardcoverStyleType.QITA.getValue().longValue()) {
        		  hql.append(" and n.houseStyle.id not in (  " + HardcoverStyleType.getOther() + ") ");
        	} else {
        		  hql.append(" and n.houseStyle.id = ? ");
                  paras.add(dto.getStyleID());
        	}
          
        }
        if (dto.getRecommon() != null) {
            hql.append(" and n.recommon = ? ");
            paras.add(dto.getRecommon());
        }
        hql.append(" order by n.id desc ");
        return super.find(hql.toString(), paras.toArray(), page);
    }
    
    public BrandStyle findOne(BrandStyleDTO dto) {
		Page page = new Page();
		page.setPageSize(1);
		page.setPage(1);
		
		List<BrandStyle> list = this.find(dto, page);
	    return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }
}
